<template>
    <el-card>
        <el-table border :data="skuArr" style="margin:10px 0px;">
            <el-table-column label="序号" type="index" width="80px" align="center">
            </el-table-column>
            <el-table-column label="名称" show-overflow-tooltip width="250px" prop="skuName">
            </el-table-column>
            <el-table-column label="描述" show-overflow-tooltip width="250px" prop="skuDesc">
            </el-table-column>
            <el-table-column label="默认图片" width="250px">
                <template #="{ row, $index }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="重量(g)" width="250px" prop="weight">
            </el-table-column>
            <el-table-column label="价格(元)" width="250px" prop="price">
            </el-table-column>
            <el-table-column label="操作" width="400px" fixed="right">
                <template #="{ row, $index }">
                    <el-button type="primary" size="small" :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
                        @click="updateSale(row)"></el-button>
                    <el-button type="primary" size="small" icon="Edit" @click="updateSku"></el-button>
                    <el-button type="primary" size="small" icon="InfoFilled" @click="findSku(row)"></el-button>
                    <el-popconfirm :title="`你确认删除${row.skuName}吗？`" @confirm="removeSku(row.id)">
                        <template #reference>
                            <el-button icon="Delete" type="primary" size="small"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 
         @current-change="handleCurrentChange" ----页面发送变化触发的事件,会自动注入pageNo-->
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pagesize" :page-sizes="[10, 20, 30, 40]"
            :background="true" layout=" prev, pager, next, jumper,->,total, sizes" :total="total" @size-change="handler"
            @current-change="getSkuList" />
        <!-- 抽屉组件 -->

        <el-drawer v-model="drawer">
            <template #header>
                <h4>查看商品的详情</h4>
            </template>
            <template #default>
                <!-- 一行两列 -->
                <el-row style="margin: 10px 0px;">
                    <el-col :span="6">名称</el-col>
                    <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                </el-row>
                <el-row style="margin: 10px 0px;">
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                </el-row>
                <el-row style="margin: 10px 0px;">
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{ skuInfo.price }}</el-col>
                </el-row>
                <el-row style="margin: 10px 0px;">
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin: 0px 5px;" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{
                            item.valueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin: 10px 0px;">
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin: 0px 5px;" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">{{
                            item.saleAttrValueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin: 10px 0px;">
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="300px">
                            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item">
                                <img :src="item.imgUrl" alt="" style="width: 100%;height: 100%;">
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>
            </template>
        </el-drawer>
    </el-card>

</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { reqSkuList, reqSaleSku, reqCancelSale, reqSkuInfo, reqRemoveSku } from '@/api/product/sku';
import { SkuResponseData, SkuData, SkuInfoData } from '@/api/product/sku/type';
import { ElMessage } from 'element-plus';
let pageNo = ref<number>(1);
let pagesize = ref<number>(10);
// 数据总条数
let total = ref<number>(0);
let skuArr = ref<SkuData[]>([]);
let skuInfo = ref<any>({});
onMounted(() => {
    getSkuList();
});
// 控制抽屉
const drawer = ref<boolean>(false)
// 获取sku数据
const getSkuList = async (pager = 1) => {
    // 收集数据
    pageNo.value = pager;//没有这句也不影响

    let result: SkuResponseData = await reqSkuList(pageNo.value, pagesize.value);
    if (result.code == 200) {
        total.value = result.data.total;
        skuArr.value = result.data.records;
    }
}
// 一页条数发送变化触发的函数
const handler = (pageSize: number) => {
    getSkuList();//无参数,默认第一页
}
// 上架下架商品按钮触发的函数
const updateSale = async (row: SkuData) => {
    if (row.isSale == 1) {
        //下架操作
        await reqCancelSale((row.id as number));
        ElMessage({ type: 'success', message: '下架成功' })
        // 更新
        getSkuList(pageNo.value)

    } else {
        // 上架操作
        await reqSaleSku((row.id as number));
        ElMessage({ type: 'success', message: '上架成功' });
        // 更新
        getSkuList(pageNo.value)
    }


}
// 更新
const updateSku = () => {
    ElMessage({ type: 'success', message: '程序员在努力的更新中....' })
}
// 查看详情数据
const findSku = async (row: SkuData) => {
    drawer.value = true;
    let result: SkuInfoData = await reqSkuInfo((row.id as number));
    skuInfo.value = result.data;
}
// 删除Sku
const removeSku = async (id: number) => {
    let result = await reqRemoveSku(id);
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: '删除成功'
        })
        // 获取数据
        getSkuList(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
    } else {
        ElMessage({
            type: 'error',
            message: '删除失败'
        })
    }
}
</script>

<style scoped>
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
</style>